package com.zixuan.gulicollege.eduservice.controller;


import com.zixuan.gulicollege.common_utils.R;
import com.zixuan.gulicollege.common_utils.ResultCode;
import com.zixuan.gulicollege.eduservice.client.VodClient;
import com.zixuan.gulicollege.eduservice.service.EduVideoService;
import com.zixuan.gulicollege.entity.eduservice.EduVideo;
import com.zixuan.gulicollege.exception_handler.ConstantExceptionInfo;
import com.zixuan.gulicollege.exception_handler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author zixuan
 * @since 2021-05-08
 */
@RestController
@RequestMapping("/edu_service/video")
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;

    /**
     * 添加小结
     * @param eduVideo
     * @return
     */
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    /**
     * 删除小结
     */
    @DeleteMapping("/deleteVideo/{videoId}")
    public R deleteVideo(@PathVariable String videoId){
        // 通过videoId查询数据获取视频id
        EduVideo eduVideo = eduVideoService.getById(videoId);
        String videoSourceId = eduVideo.getVideoSourceId();
        if (videoSourceId != null) {
            R result = vodClient.deleteAliyunVideo(videoSourceId);
            if (Objects.equals(result.getCode(), ResultCode.ERROR)) {
                throw new GuliException(ResultCode.ERROR, ConstantExceptionInfo.FAIL_DELETE_VIDEO);
            }
        }
        eduVideoService.removeById(videoId);
        return R.ok();
    }

    /**
     * 修改小结
     */
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }

    /**
     * 查询小结
     */
    @GetMapping("/getVideo/{id}")
    public R getVideo(@PathVariable String id) {
        EduVideo video = eduVideoService.getById(id);
        return R.ok().data("video", video);
    }



}

